package com.bibrApp.difu.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class UIUtils {

    public static int getWidth(Context context){
        int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
        return widthPixels;
    }

    public static int dpToPixels(Context c, int dp) {
        return (int) (c.getResources().getDisplayMetrics().density * dp);
    }

    public static int pixelsToDp(Context c, int pixel) {
        return (int) ((float) pixel / c.getResources().getDisplayMetrics().density);
    }

    public static void backgroundAlpha(AppCompatActivity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        context.getWindow().setAttributes(lp);
    }
    @SuppressLint("NewApi")
    public static void setDrawableTop(TextView textView, int drawable, int color) {
        Drawable top = textView.getContext().getResources().getDrawable(drawable);
        top.setTint(color);
        textView.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);
    }
    public static void setDrawableTop(TextView textView, int drawable) {
        Drawable top = textView.getContext().getResources().getDrawable(drawable);
        textView.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);
    }

    public static void setDrawableRight(TextView textView, int drawable) {
        Drawable top = textView.getContext().getResources().getDrawable(drawable);
        textView.setCompoundDrawablesWithIntrinsicBounds(null, null, top, null);
    }

    public static void setNullDrawable(TextView textView) {
        textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
    }

    public static void setDrawableLeft(TextView textView, int resId) {
        Resources resources = textView.getContext().getResources();
        BitmapDrawable drawable = new BitmapDrawable(resources, BitmapFactory.decodeResource(resources, resId));
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(drawable, null, null, null);
    }


    public static void appendText(TextView textView, String text, String color) {
        if (textView == null || TextUtils.isEmpty(text) || TextUtils.isEmpty(color)) {
            return;
        }
        SpannableString string = new SpannableString(text);
        string.setSpan(new ForegroundColorSpan(Color.parseColor(color)), 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.append(string);
    }

    public static void appendText(TextView textView, String text, String color, View.OnClickListener listener) {
        if (textView == null || TextUtils.isEmpty(text) || TextUtils.isEmpty(color)) {
            return;
        }
        SpannableString string = new SpannableString(text);
        string.setSpan(new ForegroundColorSpan(Color.parseColor(color)), 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        string.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                if (listener != null) {
                    listener.onClick(view);
                }
            }
        }, 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.append(string);
    }

}